let scene, mesh, camera, renderer;

~(function () {
    // 创建场景对象Scene
    scene = new THREE.Scene();

    // 半透明材质：漫反射
    let lambertMaterial = new THREE.MeshLambertMaterial({
        color: 0x0000ff
    });
    // 高光效果材质：镜面反射
    let sphereMaterial = new THREE.MeshPhongMaterial({
        color: 0x0000ff,
        specular: 0x4488ee,
        shininess: 12
    });
    // 基础网络材质
    let basicMaterial = new THREE.MeshBasicMaterial({
        // 三角面颜色
        color: 0x0000ff,
        // 两面可见
        side: THREE.DoubleSide
    });
    // PBR物理材质
    let standardMaterial = new THREE.MeshStandardMaterial({});

    // 创建一个立方体几何对象Geometry
    let box = new THREE.BoxGeometry(100, 100, 100);
    // 网格模型对象Mesh
    mesh = new THREE.Mesh(box, lambertMaterial);
    // 网格模型添加到场景中
    scene.add(mesh);
    // 球体 参数：半径60  经纬度细分数40,40
    let sphere = new THREE.SphereGeometry(60, 40, 40);
    let mesh2 = new THREE.Mesh(sphere, sphereMaterial);
    mesh2.translateY(120);
    scene.add(mesh2);
    // 正八面体
    let octahedron = new THREE.OctahedronGeometry(50);
    let mesh3 = new THREE.Mesh(octahedron, basicMaterial);
    mesh3.position.set(120, 100, 0);
    scene.add(mesh3);
    // 圆柱  参数：圆柱面顶部、底部直径50,50   高度100  圆周分段数
    let cylinder = new THREE.CylinderGeometry(50, 50, 100, 25);
    let mesh4 = new THREE.Mesh(cylinder, standardMaterial);
    mesh4.position.set(120, 0, 0);
    scene.add(mesh4);
    // 创建管道成型的路径(3D样条曲线)
    let path = new THREE.CatmullRomCurve3([
        new THREE.Vector3(-10, -50, -50),
        new THREE.Vector3(10, 0, 0),
        new THREE.Vector3(8, 50, 50),
        new THREE.Vector3(-5, 0, 100)
    ]);
    // path:路径   40：沿着轨迹细分数  2：管道半径   25：管道截面圆细分数
    let tube = new THREE.TubeGeometry(path, 40, 2, 25);
    let mesh5 = new THREE.Mesh(tube, standardMaterial);
    mesh5.position.set(120, 120, 0);
    scene.add(mesh5);

    // 点光源
    let point = new THREE.PointLight(0xffffff);
    // 点光源位置
    point.position.set(400, 200, 300);
    // 点光源添加到场景中
    scene.add(point);
    // 点光源2
    let point2 = new THREE.PointLight(0xffffff);
    point2.position.set(-400, -200, -300);
    scene.add(point2);

    // 环境光
    let ambient = new THREE.AmbientLight(0x444444);
    // 环境光添加到场景中
    scene.add(ambient);

    // 相机窗口宽度
    let width = window.innerWidth;
    // 相机窗口高度
    let height = window.innerHeight;
    // 相机窗口宽高比
    let k = width / height;
    // 三维场景显示范围控制系数，系数越大，显示的范围越大
    let s = 200;
    //创建相机对象
    camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
    // 设置相机位置
    camera.position.set(200, 300, 200);
    // 设置相机方向(指向的场景对象)
    camera.lookAt(scene.position);

    // 渲染器对象
    renderer = new THREE.WebGLRenderer();
    // 设置渲染区域尺寸
    renderer.setSize(width, height);
    // 设置背景颜色
    renderer.setClearColor(0xb9d3ff, 1);
    // body元素中插入canvas对象
    document.body.appendChild(renderer.domElement);

    render();

    // 创建控件对象
    let controls = new THREE.OrbitControls(camera, renderer.domElement);
    // 监听鼠标、键盘事件: 与requestAnimationFrame渲染冲突
    // controls.addEventListener('change', render);

    // 辅助坐标系  参数250表示坐标系大小，可以根据场景大小去设置
    let axesHelper = new THREE.AxesHelper(250);
    scene.add(axesHelper);
})();

/**
 * 添加纹理
 */
function doTexture() {
    // 创建一个立方体几何对象Geometry
    let box = new THREE.BoxGeometry(100, 100, 100);
    // TextureLoader创建一个纹理加载器对象，可以加载图片作为几何体纹理
    let textureLoader = new THREE.TextureLoader();
    // 执行load方法，加载纹理贴图成功后，返回一个纹理对象Texture
    textureLoader.load('/images/wenli-1.jpg', function (texture) {
        // 材质对象Material
        let material = new THREE.MeshLambertMaterial({
            // 设置颜色纹理贴图：Texture对象作为材质map属性的属性值
            // color: 0x0000ff,
            // 设置颜色贴图属性值
            map: texture
        });
        // 网格模型对象Mesh
        let mesh = new THREE.Mesh(box, material);
        mesh.position.set(200, 120, 0);
        // 网格模型添加到场景中
        scene.add(mesh);

        // 纹理贴图加载成功后，调用渲染函数执行渲染操作
        render();
    })
}

/**
 * 渲染
 */
function render() {
    // 执行渲染操作
    renderer.render(scene, camera);
    // 每次绕y轴旋转0.01弧度
    mesh.rotateY(0.01);

    requestAnimationFrame(render);
}

function goTube() {
    window.location.href = '/tube';
}